Hanye官网
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. <template>
  2. <div>
  3. <div class="w-full h-[55px] sm:h-[72px]"></div>
  4. <ErrorBoundary :error="error">
  5. <div v-if="isLoading" class="flex justify-center py-12">
  6. <!-- 加载中 -->
  7. <div
  8. class="animate-spin h-8 w-8 border-4 border-cyan-400 rounded-full border-t-transparent"
  9. ></div>
  10. </div>
  11. <div v-else>
  12. <!-- 面包屑导航 -->
  13. <div class="max-w-full mb-6 xl:px-2 lg:px-2 md:px-4 px-4 mt-6">
  14. <div class="max-w-screen-2xl mx-auto">
  15. <nuxt-link
  16. :to="`${homepagePath}/`"
  17. class="justify-start text-white/60 text-base font-normal"
  18. >{{ t("common.breadcrumb.home") }}</nuxt-link
  19. >
  20. <span class="text-white/60 text-base font-normal px-2"> / </span>
  21. <nuxt-link
  22. :to="`${homepagePath}/products`"
  23. class="text-white/60 text-base font-normal"
  24. >{{ t("common.breadcrumb.products") }}</nuxt-link
  25. >
  26. <span class="text-white/60 text-base font-normal px-2"> / </span>
  27. <nuxt-link
  28. v-if="product?.category"
  29. :to="`${homepagePath}/products?category=${encodeURIComponent(
  30. product.category
  31. )}`"
  32. class="text-white/60 text-base font-normal"
  33. >{{ product.category }}</nuxt-link
  34. >
  35. <span class="text-white/60 text-base font-normal px-2"> / </span>
  36. <span class="text-white text-base font-normal">{{
  37. product?.title || product?.name
  38. }}</span>
  39. </div>
  40. </div>
  41. <!-- 产品详情内容 -->
  42. <div
  43. v-if="product"
  44. class="max-w-full mb-12 md:mb-20 lg:mb-32 xl:px-2 lg:px-2 md:px-4 px-4"
  45. >
  46. <div class="max-w-screen-2xl mx-auto">
  47. <div class="grid grid-cols-1 lg:grid-cols-2 gap-8 lg:gap-16">
  48. <!-- 左侧产品图片 -->
  49. <div
  50. class="flex flex-col gap-6 lg:sticky lg:top-24 self-start select-none"
  51. >
  52. <!-- 主图展示 -->
  53. <div
  54. class="bg-zinc-900 rounded-lg p-8 relative overflow-hidden group aspect-square"
  55. >
  56. <!-- 主图容器 - Swiper -->
  57. <Swiper
  58. :modules="[Navigation, Pagination, EffectFade, Thumbs]"
  59. :slides-per-view="1"
  60. :pagination="{
  61. clickable: true,
  62. dynamicBullets: true,
  63. dynamicMainBullets: 3,
  64. }"
  65. :navigation="true"
  66. :effect="'fade'"
  67. :fade-effect="{ crossFade: true }"
  68. :thumbs="{ swiper: thumbsSwiper }"
  69. class="w-full h-full product-swiper"
  70. @swiper="swiperInstance = $event"
  71. @slideChange="currentSlideIndex = $event.activeIndex"
  72. >
  73. <SwiperSlide
  74. v-for="(image, slideIndex) in [
  75. product.image,
  76. ...(product.gallery || []),
  77. ]"
  78. :key="slideIndex"
  79. >
  80. <div class="relative w-full h-full">
  81. <!-- 当前图片 -->
  82. <img
  83. :src="image"
  84. :alt="`${product.name} - ${t('products.image', {
  85. index: slideIndex + 1,
  86. })}`"
  87. class="w-full h-full object-contain rounded-lg transition-all duration-500"
  88. :class="{
  89. 'opacity-0': isSlideThumbnailLoading[slideIndex],
  90. 'opacity-100':
  91. !isSlideThumbnailLoading[slideIndex] &&
  92. !slideThumbnailErrors[slideIndex],
  93. }"
  94. @load="handleSlideImageLoad(slideIndex)"
  95. @error="handleSlideImageError(slideIndex)"
  96. />
  97. <!-- 加载状态 -->
  98. <div
  99. v-if="isSlideThumbnailLoading[slideIndex]"
  100. class="absolute inset-0 flex items-center justify-center bg-zinc-800/50 z-10"
  101. >
  102. <div
  103. class="animate-spin h-8 w-8 border-4 border-cyan-400 rounded-full border-t-transparent"
  104. ></div>
  105. </div>
  106. <!-- 错误提示 -->
  107. <div
  108. v-if="slideThumbnailErrors[slideIndex]"
  109. class="absolute inset-0 flex items-center justify-center bg-red-900/50 z-20"
  110. >
  111. <div class="flex flex-col items-center gap-2">
  112. <span class="text-white">{{
  113. t("products.imageLoadError")
  114. }}</span>
  115. <button
  116. @click.stop="retryLoadSlideImage(slideIndex)"
  117. class="px-4 py-2 bg-cyan-400 text-white rounded-lg hover:bg-cyan-600 transition-colors duration-300"
  118. >
  119. {{ t("products.retry") }}
  120. </button>
  121. </div>
  122. </div>
  123. </div>
  124. </SwiperSlide>
  125. </Swiper>
  126. <!-- 全局加载状态 - 仅在所有图片都未加载完成时显示 -->
  127. <div
  128. v-if="isInitialLoading"
  129. class="absolute inset-0 flex items-center justify-center bg-zinc-900/80 z-30"
  130. >
  131. <div
  132. class="animate-spin h-12 w-12 border-4 border-cyan-400 rounded-full border-t-transparent"
  133. ></div>
  134. </div>
  135. </div>
  136. <!-- 缩略图列表 - Swiper -->
  137. <div
  138. class="relative bg-zinc-900 rounded-lg py-6 px-8 overflow-hidden product-thumbnail-container"
  139. >
  140. <Swiper
  141. :modules="[Navigation, Thumbs, FreeMode]"
  142. :slides-per-view="'auto'"
  143. :space-between="12"
  144. :free-mode="true"
  145. :watch-slides-progress="true"
  146. :navigation="{
  147. nextEl: '.swiper-thumb-next',
  148. prevEl: '.swiper-thumb-prev',
  149. }"
  150. class="thumbs-swiper"
  151. @swiper="thumbsSwiper = $event"
  152. >
  153. <SwiperSlide
  154. v-for="(image, index) in [
  155. product.image,
  156. ...(product.gallery || []),
  157. ]"
  158. :key="index"
  159. class="!w-16 !h-16 md:!w-20 md:!h-20 !flex-shrink-0 cursor-pointer transition-all duration-300 relative"
  160. >
  161. <div
  162. class="w-full h-full rounded-lg overflow-hidden thumbnail-fixed-size"
  163. :class="{
  164. 'ring-2 ring-cyan-400 ring-offset-2 ring-offset-zinc-900':
  165. currentSlideIndex === index,
  166. 'hover:ring-1 hover:ring-cyan-400/50 hover:ring-offset-1 hover:ring-offset-zinc-900':
  167. currentSlideIndex !== index,
  168. 'opacity-50':
  169. isSlideThumbnailLoading[index] ||
  170. slideThumbnailErrors[index],
  171. }"
  172. >
  173. <!-- 缩略图加载状态 -->
  174. <div
  175. v-if="isSlideThumbnailLoading[index]"
  176. class="absolute inset-0 flex items-center justify-center bg-zinc-800/50 rounded-lg z-10"
  177. >
  178. <div
  179. class="animate-spin h-4 w-4 border-2 border-cyan-400 rounded-full border-t-transparent"
  180. ></div>
  181. </div>
  182. <!-- 缩略图遮罩 -->
  183. <div
  184. class="absolute inset-0 transition-all duration-300 rounded-lg"
  185. :class="{
  186. 'bg-black/30': currentSlideIndex === index,
  187. 'bg-black/0 hover:bg-black/20':
  188. currentSlideIndex !== index,
  189. }"
  190. ></div>
  191. <img
  192. :src="image"
  193. :alt="`${product.name} - ${t('products.image', {
  194. index: index + 1,
  195. })}`"
  196. class="w-full h-full object-cover transition-all duration-300 rounded-lg"
  197. :class="{
  198. 'opacity-0': isSlideThumbnailLoading[index],
  199. 'opacity-100':
  200. !isSlideThumbnailLoading[index] &&
  201. !slideThumbnailErrors[index],
  202. 'hover:scale-110': currentSlideIndex !== index,
  203. }"
  204. @load="handleSlideImageLoad(index)"
  205. @error="handleSlideImageError(index)"
  206. />
  207. <!-- 缩略图错误提示 -->
  208. <div
  209. v-if="slideThumbnailErrors[index]"
  210. class="absolute inset-0 flex items-center justify-center bg-red-900/50 rounded-lg"
  211. >
  212. <div class="flex flex-col items-center gap-1">
  213. <span class="text-white text-xs">{{
  214. t("products.error")
  215. }}</span>
  216. <button
  217. @click.stop="retryLoadSlideImage(index)"
  218. class="px-2 py-1 bg-cyan-400 text-white text-xs rounded hover:bg-cyan-600 transition-colors duration-300"
  219. >
  220. {{ t("products.retry") }}
  221. </button>
  222. </div>
  223. </div>
  224. </div>
  225. </SwiperSlide>
  226. </Swiper>
  227. <!-- 缩略图导航按钮 -->
  228. <button
  229. class="swiper-thumb-prev absolute top-1/2 left-2 z-10 w-8 h-8 flex items-center justify-center bg-black/50 hover:bg-cyan-400 rounded-full transform -translate-y-1/2 transition-all duration-300"
  230. >
  231. <svg
  232. xmlns="http://www.w3.org/2000/svg"
  233. class="h-5 w-5 text-white"
  234. fill="none"
  235. viewBox="0 0 24 24"
  236. stroke="currentColor"
  237. >
  238. <path
  239. stroke-linecap="round"
  240. stroke-linejoin="round"
  241. stroke-width="2"
  242. d="M15 19l-7-7 7-7"
  243. />
  244. </svg>
  245. </button>
  246. <button
  247. class="swiper-thumb-next absolute top-1/2 right-2 z-10 w-8 h-8 flex items-center justify-center bg-black/50 hover:bg-cyan-400 rounded-full transform -translate-y-1/2 transition-all duration-300"
  248. >
  249. <svg
  250. xmlns="http://www.w3.org/2000/svg"
  251. class="h-5 w-5 text-white"
  252. fill="none"
  253. viewBox="0 0 24 24"
  254. stroke="currentColor"
  255. >
  256. <path
  257. stroke-linecap="round"
  258. stroke-linejoin="round"
  259. stroke-width="2"
  260. d="M9 5l7 7-7 7"
  261. />
  262. </svg>
  263. </button>
  264. </div>
  265. </div>
  266. <!-- 右侧产品信息 -->
  267. <div class="flex flex-col gap-8">
  268. <!-- 产品名称 -->
  269. <div class="bg-zinc-900 rounded-lg p-6">
  270. <h1 class="text-white text-3xl font-medium mb-4">
  271. {{ product.title || product.name }}
  272. </h1>
  273. <div class="text-[#71717A] text-lg leading-relaxed">
  274. {{ product.summary }}
  275. </div>
  276. </div>
  277. <!-- 产品参数 -->
  278. <div class="bg-zinc-900 rounded-lg p-6">
  279. <div class="grid grid-cols-1 gap-4">
  280. <div
  281. class="flex justify-between items-center py-2 border-b border-zinc-800"
  282. >
  283. <span class="text-[#71717A]">{{
  284. t("products.categoryTitle")
  285. }}</span>
  286. <span class="text-white font-medium">{{
  287. product.category
  288. }}</span>
  289. </div>
  290. <div
  291. class="flex justify-between items-center py-2 border-b border-zinc-800"
  292. >
  293. <span class="text-[#71717A]">{{
  294. t("products.usageTitle")
  295. }}</span>
  296. <span class="text-white font-medium">{{
  297. product.usage?.join(", ")
  298. }}</span>
  299. </div>
  300. <div
  301. v-if="product.capacities && product.capacities.length > 0"
  302. class="flex justify-between items-center py-2"
  303. >
  304. <span class="text-[#71717A]">{{
  305. t("products.capacitiesTitle")
  306. }}</span>
  307. <span class="text-white font-medium">{{
  308. product.capacities?.join(" / ")
  309. }}</span>
  310. </div>
  311. </div>
  312. </div>
  313. <!-- 产品描述 -->
  314. <div
  315. v-if="product.description"
  316. class="bg-zinc-900 rounded-lg p-6"
  317. >
  318. <h2 class="text-white text-xl font-medium mb-6">
  319. {{ t("products.productDescription") }}
  320. </h2>
  321. <div
  322. class="text-[#71717A] leading-relaxed space-y-4 prose prose-invert max-w-none"
  323. >
  324. {{ product.description }}
  325. </div>
  326. </div>
  327. <div class="bg-zinc-900 rounded-lg p-6">
  328. <div
  329. class="text-[#71717A] leading-relaxed space-y-4 prose prose-invert max-w-none"
  330. >
  331. <ContentRenderer :value="product.content" />
  332. </div>
  333. </div>
  334. <!-- 相关产品 -->
  335. <div
  336. v-if="relatedProducts.length > 0"
  337. class="bg-zinc-900 rounded-lg p-6"
  338. >
  339. <h2 class="text-white text-xl font-medium mb-6">
  340. {{ t("products.relatedProducts") }}
  341. </h2>
  342. <div
  343. class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4"
  344. >
  345. <nuxt-link
  346. v-for="relatedProduct in relatedProducts"
  347. :key="relatedProduct.id"
  348. :to="`${homepagePath}/products/${relatedProduct.id}`"
  349. class="group"
  350. >
  351. <div
  352. class="bg-zinc-800 rounded-lg p-4 transition-all duration-300 hover:bg-zinc-700"
  353. >
  354. <div
  355. class="aspect-square mb-4 overflow-hidden rounded-lg"
  356. >
  357. <img
  358. :src="relatedProduct.image"
  359. :alt="relatedProduct.title || relatedProduct.name"
  360. class="w-full h-full object-cover transition-transform duration-300 group-hover:scale-110"
  361. />
  362. </div>
  363. <h3
  364. class="text-white text-lg font-medium mb-2 line-clamp-2"
  365. >
  366. {{ relatedProduct.title || relatedProduct.name }}
  367. </h3>
  368. <p class="text-[#71717A] text-sm line-clamp-2">
  369. {{ relatedProduct.summary }}
  370. </p>
  371. </div>
  372. </nuxt-link>
  373. </div>
  374. </div>
  375. </div>
  376. </div>
  377. </div>
  378. </div>
  379. </div>
  380. </ErrorBoundary>
  381. </div>
  382. </template>
  383. <script setup lang="ts">
  384. /**
  385. * 产品详情页面
  386. * 展示产品主图、参数和描述
  387. */
  388. import { useErrorHandler } from "~/composables/useErrorHandler";
  389. import { useRoute, useI18n, useAsyncData } from "#imports";
  390. import { queryCollection } from "#imports";
  391. import { ContentRenderer } from "#components";
  392. import { Swiper, SwiperSlide } from "swiper/vue";
  393. import {
  394. Navigation,
  395. Pagination,
  396. EffectFade,
  397. Thumbs,
  398. FreeMode,
  399. } from "swiper/modules";
  400. import type { Swiper as SwiperType } from "swiper";
  401. import "swiper/css";
  402. import "swiper/css/navigation";
  403. import "swiper/css/pagination";
  404. import "swiper/css/effect-fade";
  405. const { error, isLoading } = useErrorHandler();
  406. const route = useRoute();
  407. const { locale, t } = useI18n();
  408. const id = route.params.id as string;
  409. const swiperInstance = ref<SwiperType | null>(null);
  410. const thumbsSwiper = ref<SwiperType | null>(null);
  411. // 图片状态
  412. const currentSlideIndex = ref(0);
  413. const isInitialLoading = ref(true); // 初始加载状态
  414. const isSlideThumbnailLoading = ref<boolean[]>([]);
  415. const slideThumbnailErrors = ref<boolean[]>([]);
  416. // 滚动跟随相关
  417. const scrollContainer = ref<HTMLElement | null>(null);
  418. const isSticky = ref(false);
  419. const homepagePath = computed(() => {
  420. return locale.value === "zh" ? "" : `/${locale.value}`;
  421. });
  422. interface Product {
  423. id: string;
  424. name: string;
  425. usage: string[];
  426. capacities: string[];
  427. category: string;
  428. categoryId: string;
  429. description: string;
  430. summary: string;
  431. image: string;
  432. gallery: string[];
  433. body: string;
  434. content?: any;
  435. meta?: {
  436. series?: string[];
  437. name?: string;
  438. title?: string;
  439. image?: string;
  440. summary?: string;
  441. };
  442. title?: string;
  443. }
  444. /**
  445. * 使用queryCollection获取产品数据
  446. */
  447. const { data: productContent } = await useAsyncData(
  448. `product-${id}`,
  449. async () => {
  450. try {
  451. // 使用queryCollection从content目录获取数据
  452. const content = await queryCollection("content")
  453. .where("path", "LIKE", `/products/${locale.value}/${id}`)
  454. .first();
  455. return content;
  456. } catch (err) {
  457. console.error("Error fetching product content:", err);
  458. error.value = new Error(t("products.loadError"));
  459. return null;
  460. }
  461. }
  462. );
  463. /**
  464. * 获取分类信息
  465. */
  466. const { data: categoryContent } = await useAsyncData(
  467. `category-${productContent.value?.meta?.categoryId}`,
  468. async () => {
  469. if (!productContent.value?.meta?.categoryId) return null;
  470. try {
  471. const content = await queryCollection("content")
  472. .where(
  473. "path",
  474. "LIKE",
  475. `/categories/${locale.value}/${productContent.value.meta?.categoryId}`
  476. )
  477. .first();
  478. return content;
  479. } catch (err) {
  480. console.error("Error fetching category:", err);
  481. return null;
  482. }
  483. },
  484. {
  485. immediate: !!productContent.value?.meta?.categoryId,
  486. }
  487. );
  488. /**
  489. * 使用计算属性解析产品数据
  490. */
  491. const product = computed<Product | null>(() => {
  492. if (!productContent.value) return null;
  493. // 提取产品数据
  494. const meta = productContent.value.meta || {};
  495. return {
  496. id: id,
  497. name: String(meta.name || productContent.value.title || ""),
  498. title: String(productContent.value.title || meta.name || ""),
  499. usage: Array.isArray(meta.usage) ? meta.usage : [],
  500. capacities: Array.isArray(meta.capacities) ? meta.capacities : [],
  501. category: categoryContent.value?.title || "",
  502. categoryId: meta.categoryId || "",
  503. description: productContent.value.description || "",
  504. summary: String(meta.summary || ""),
  505. image: String(meta.image || ""),
  506. gallery: Array.isArray(meta.gallery) ? meta.gallery : [],
  507. body: productContent.value.body || "",
  508. content: productContent.value,
  509. meta: {
  510. series: Array.isArray(meta.series) ? meta.series : [],
  511. name: String(meta.name || ""),
  512. title: String(productContent.value.title || ""),
  513. image: String(meta.image || ""),
  514. summary: String(meta.summary || ""),
  515. },
  516. };
  517. });
  518. /**
  519. * 获取相关产品
  520. */
  521. const { data: relatedProductsContent } = await useAsyncData(
  522. `related-products-${id}`,
  523. async () => {
  524. try {
  525. // 获取产品列表
  526. const content = await queryCollection("content")
  527. .where("path", "LIKE", `/products/${locale.value}/%`)
  528. .all();
  529. return content;
  530. } catch (err) {
  531. console.error("Error fetching related products:", err);
  532. return [];
  533. }
  534. }
  535. );
  536. /**
  537. * 处理相关产品数据
  538. */
  539. const relatedProducts = computed(() => {
  540. if (!relatedProductsContent.value || !product.value) return [];
  541. // 获取当前产品的分类和系列
  542. const currentCategory = product.value.categoryId;
  543. const currentSeries = product.value.meta?.series || [];
  544. const currentProductId = id;
  545. return relatedProductsContent.value
  546. .filter((item: any) => {
  547. // 排除当前产品 - 多重检查确保排除
  548. if (item._path === `/products/${locale.value}/${id}`) return false;
  549. const meta = item.meta || {};
  550. if (meta.name === currentProductId) return false;
  551. const itemCategoryId = meta.categoryId || "";
  552. const itemSeries = Array.isArray(meta.series) ? meta.series : [];
  553. // 判断是否同类别或同系列
  554. const isSameCategory =
  555. currentCategory && itemCategoryId === currentCategory;
  556. const hasSameSeries =
  557. currentSeries.length > 0 &&
  558. itemSeries.some((series: string) => currentSeries.includes(series));
  559. // 返回同类别或同系列的产品
  560. return isSameCategory || hasSameSeries;
  561. })
  562. .map((item: any) => {
  563. const meta = item.meta || {};
  564. return {
  565. id: meta.name || "",
  566. name: meta.name || item.title || "",
  567. title: item.title || meta.name || "",
  568. image: meta.image || "",
  569. summary: meta.summary || "",
  570. category: meta.categoryId || "",
  571. series: Array.isArray(meta.series) ? meta.series : [],
  572. };
  573. })
  574. .slice(0, 6); // 最多显示6个相关产品
  575. });
  576. /**
  577. * 预加载下一张图片
  578. */
  579. function preloadNextImage(image: string) {
  580. currentSlideIndex.value =
  581. (currentSlideIndex.value + 1) % (product.value?.gallery?.length || 1);
  582. }
  583. /**
  584. * 处理图片加载完成
  585. */
  586. function handleSlideImageLoad(index: number) {
  587. isSlideThumbnailLoading.value[index] = false;
  588. slideThumbnailErrors.value[index] = false;
  589. // 检查是否所有图片都已加载
  590. const allImagesLoaded = isSlideThumbnailLoading.value.every(
  591. (status) => !status
  592. );
  593. if (allImagesLoaded) {
  594. isInitialLoading.value = false;
  595. }
  596. }
  597. /**
  598. * 处理图片加载错误
  599. */
  600. function handleSlideImageError(index: number) {
  601. isSlideThumbnailLoading.value[index] = false;
  602. slideThumbnailErrors.value[index] = true;
  603. }
  604. /**
  605. * 重试加载图片
  606. */
  607. function retryLoadSlideImage(index: number) {
  608. isSlideThumbnailLoading.value[index] = true;
  609. slideThumbnailErrors.value[index] = false;
  610. // 确定正确的图片URL
  611. const images = [product.value?.image, ...(product.value?.gallery || [])];
  612. const imageUrl = images[index];
  613. // 检查图片URL是否有效
  614. if (!imageUrl) {
  615. console.error("Invalid image URL:", index);
  616. slideThumbnailErrors.value[index] = true;
  617. isSlideThumbnailLoading.value[index] = false;
  618. return;
  619. }
  620. // 创建新的图片对象并设置超时
  621. const img = new Image();
  622. const timeoutId = setTimeout(() => {
  623. handleSlideImageError(index);
  624. }, 10000); // 10秒超时
  625. img.onload = () => {
  626. clearTimeout(timeoutId);
  627. handleSlideImageLoad(index);
  628. };
  629. img.onerror = (error) => {
  630. clearTimeout(timeoutId);
  631. console.error("Image load error:", { index, error });
  632. handleSlideImageError(index);
  633. };
  634. // 设置跨域属性
  635. img.crossOrigin = "anonymous";
  636. // 最后设置src以开始加载
  637. img.src = imageUrl;
  638. }
  639. // 页面加载时初始化状态
  640. onMounted(() => {
  641. // 初始化缩略图加载状态数组
  642. const galleryLength = (product.value?.gallery?.length || 0) + 1;
  643. isSlideThumbnailLoading.value = new Array(galleryLength).fill(true);
  644. slideThumbnailErrors.value = new Array(galleryLength).fill(false);
  645. // 设置初始加载状态
  646. isInitialLoading.value = true;
  647. // 预加载所有缩略图
  648. const images = [product.value?.image, ...(product.value?.gallery || [])];
  649. images.forEach((image, index) => {
  650. if (image) {
  651. const img = new Image();
  652. img.onload = () => handleSlideImageLoad(index);
  653. img.onerror = () => handleSlideImageError(index);
  654. img.src = image;
  655. }
  656. });
  657. // 添加滚动监听
  658. scrollContainer.value = document.querySelector(".max-w-screen-2xl");
  659. if (scrollContainer.value) {
  660. window.addEventListener("scroll", handleScroll, { passive: true });
  661. }
  662. });
  663. // 清理滚动监听
  664. onUnmounted(() => {
  665. if (scrollContainer.value) {
  666. window.removeEventListener("scroll", handleScroll);
  667. }
  668. });
  669. // 处理滚动事件
  670. function handleScroll() {
  671. if (!scrollContainer.value) return;
  672. const containerRect = scrollContainer.value.getBoundingClientRect();
  673. const scrollTop = window.scrollY || document.documentElement.scrollTop;
  674. // 当容器顶部距离视窗顶部小于100px时,启用sticky
  675. isSticky.value = containerRect.top < 100;
  676. }
  677. // SEO优化
  678. useHead(() => ({
  679. title: `${product.value?.name} - Hanye`,
  680. meta: [
  681. {
  682. name: "description",
  683. content: product.value?.description,
  684. },
  685. ],
  686. }));
  687. </script>
  688. <style lang="scss" scoped>
  689. /* 隐藏滚动条但保持滚动功能 */
  690. .scrollbar-hide {
  691. -ms-overflow-style: none; /* IE and Edge */
  692. scrollbar-width: none; /* Firefox */
  693. }
  694. .scrollbar-hide::-webkit-scrollbar {
  695. display: none; /* Chrome, Safari and Opera */
  696. }
  697. /* 图片过渡动画 */
  698. .main-image {
  699. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  700. }
  701. /* 缩略图悬停效果 */
  702. .thumbnail-item {
  703. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  704. }
  705. .thumbnail-item:hover {
  706. transform: translateY(-2px);
  707. }
  708. /* 缩略图选中效果 */
  709. .thumbnail-item.selected {
  710. transform: scale(1.05);
  711. box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
  712. 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  713. }
  714. /* 产品信息卡片效果 */
  715. .info-card {
  716. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  717. }
  718. .info-card:hover {
  719. transform: translateY(-2px);
  720. box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
  721. 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  722. }
  723. /* 滚动跟随效果 */
  724. .lg\:sticky {
  725. position: sticky;
  726. top: 6rem; /* 96px */
  727. transition: all 0.3s ease;
  728. z-index: 10;
  729. max-height: calc(100vh - 6rem);
  730. overflow-y: auto;
  731. }
  732. @media (max-width: 1024px) {
  733. .lg\:sticky {
  734. position: relative;
  735. top: 0;
  736. max-height: none;
  737. }
  738. }
  739. /* Swiper 样式定制 */
  740. .product-swiper {
  741. :deep(.swiper-pagination-bullet) {
  742. background-color: white;
  743. opacity: 0.5;
  744. }
  745. :deep(.swiper-pagination-bullet-active) {
  746. opacity: 1;
  747. background-color: theme("colors.cyan.400");
  748. }
  749. :deep(.swiper-button-next),
  750. :deep(.swiper-button-prev) {
  751. color: theme("colors.cyan.500");
  752. background-color: rgba(0, 0, 0, 0.3);
  753. width: 36px;
  754. height: 36px;
  755. border-radius: 50%;
  756. &:after {
  757. font-size: 16px;
  758. font-weight: bold;
  759. }
  760. &:hover {
  761. background-color: theme("colors.cyan.500");
  762. color: white;
  763. }
  764. }
  765. :deep(.swiper-button-disabled) {
  766. opacity: 0.35;
  767. cursor: auto;
  768. pointer-events: none;
  769. }
  770. :deep(.swiper-slide) {
  771. display: flex;
  772. align-items: center;
  773. justify-content: center;
  774. }
  775. }
  776. /* 缩略图Swiper样式 */
  777. .thumbs-swiper {
  778. overflow: visible;
  779. padding: 0.25rem;
  780. min-height: 6rem;
  781. :deep(.swiper-wrapper) {
  782. align-items: center;
  783. display: flex;
  784. min-height: 6rem;
  785. }
  786. :deep(.swiper-slide) {
  787. width: auto;
  788. height: auto;
  789. opacity: 0.7;
  790. transition: all 0.3s ease;
  791. display: flex;
  792. align-items: center;
  793. justify-content: center;
  794. }
  795. :deep(.swiper-slide-thumb-active) {
  796. opacity: 1;
  797. transform: scale(1.05);
  798. z-index: 1;
  799. }
  800. }
  801. /* 自定义缩略图导航按钮 */
  802. .swiper-thumb-next,
  803. .swiper-thumb-prev {
  804. &:focus {
  805. outline: none;
  806. }
  807. &.swiper-button-disabled {
  808. opacity: 0.3;
  809. cursor: default;
  810. background-color: rgba(0, 0, 0, 0.2);
  811. &:hover {
  812. background-color: rgba(0, 0, 0, 0.2);
  813. }
  814. }
  815. }
  816. /* 响应式调整 */
  817. @media (max-width: 768px) {
  818. .thumbs-swiper {
  819. padding: 0;
  820. :deep(.swiper-slide) {
  821. margin-right: 8px;
  822. }
  823. }
  824. /* 确保缩略图容器在移动设备上有足够空间 */
  825. .product-thumbnail-container {
  826. padding: 1rem;
  827. .swiper-thumb-prev {
  828. left: 0.5rem;
  829. width: 2rem;
  830. height: 2rem;
  831. }
  832. .swiper-thumb-next {
  833. right: 0.5rem;
  834. width: 2rem;
  835. height: 2rem;
  836. }
  837. }
  838. }
  839. /* 解决不同分辨率下缩略图大小问题 */
  840. .thumbnail-fixed-size {
  841. width: 100%;
  842. height: 100%;
  843. aspect-ratio: 1 / 1;
  844. position: relative;
  845. }
  846. </style>